package com.system.manager.modules.authorized.service.impl;


import com.system.manager.modules.authorized.entity.SysMenu;
import com.system.manager.modules.authorized.mapper.SysMenuRepository;
import com.system.manager.modules.authorized.service.SysMenuService;
import com.system.manager.modules.authorized.vo.MenuVo;
import com.system.manager.utils.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yxp
 * @date 2021-08-27 15:49
 **/
@Slf4j
@Transactional
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    private SysMenuRepository sysMenuRepository;

    @Override
    public Map<String, Object> menu(Integer role) {
        log.info("---->访问菜单接口");
        Map<String, Object> map = new HashMap<>(16);
        Map<String,Object> home = new HashMap<>(16);
        Map<String,Object> logo = new HashMap<>(16);
        List<SysMenu> menuList = sysMenuRepository.getSystemMenuByStatusAndSort(role);
        List<MenuVo> menuInfo = new ArrayList<>();
        for (SysMenu e : menuList) {
            MenuVo menuVO = new MenuVo();
            menuVO.setId(e.getKey().getId());
            menuVO.setPid(e.getPid());
            menuVO.setHref(e.getKey().getHref());
            menuVO.setTitle(e.getKey().getTitle());
            menuVO.setIcon(e.getIcon());
            menuVO.setTarget(e.getTarget());
            menuInfo.add(menuVO);
        }
        // 设置返回的首页内容
        home.put("title","首页");
        home.put("href", "page/welcome-1.html");//控制器路由,自行定义

        // 设置返回的图标内容
        logo.put("title","后台管理系统");
        logo.put("image","images/logo.png");//静态资源文件路径,可使用默认的logo.png
        logo.put("href", "index.html");

        // 返回给前端对应的模块url
        map.put("menuInfo", TreeUtil.toTree(menuInfo, 0L));
        map.put("homeInfo", home);
        map.put("logoInfo", logo);

        return map;
    }

}
